package com.wangxinyuchuan.demo.iterator;

/**
 * @author wangxinyuchuan
 * @date 2023/4/16 5:48 PM
 */
public class BookShelfIterator implements Iterator<Book> {

    private BookShelf bookShelf;

    private int index;

    private int end;

    public BookShelfIterator(BookShelf bookShelf) {
        this.bookShelf = bookShelf;
        this.index = 0;
        this.end = bookShelf.getLength() - 1;
    }

    @Override
    public boolean hasNext() {
        return index < bookShelf.getLength();
    }

    @Override
    public Book next() {
        Book book = bookShelf.getBookAt(index);
        index++;
        return book;
    }

    @Override
    public boolean hasPrev() {
        return end >= 0;
    }

    @Override
    public Book prev() {
        Book book = bookShelf.getBookAt(end);
        end--;
        return book;
    }
}
